<?php
namespace Admin\Controller;
use Think\Controller;
class LaoshiController extends CommonController 
{
	//老师列表
    public function index()
    {
    	if (IS_POST) {
	    	$start = strtotime(I('post.start'));
	    	$stop = strtotime(I('post.stop'));
	    	$username = I('post.username');
	    	$sql = "select id,phone,sex,name,education,school,major,city,is_shenhe_xs,is_dongjie,create_time from laoshi_user where is_del=0 and create_time between '{$start}' and '{$stop}' and name like '%{$username}%'";
	    	$users = M()->query($sql);
	    	$this->assign('users',$users);
    	}else {
	    	//分页
	        $count = M('laoshi_user')->count();
	        $this->assign('num',$count);

	        //所有用户显示出来
	        $user = M('laoshi_user');        
	        // 查询满足要求的总记录数
	        $count =  $user->count();
	        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
	        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

	        //显示
	        $sql = "select id,phone,sex,name,education,school,major,city,is_shenhe_xs,is_dongjie,create_time from laoshi_user where is_del=0 limit ".$Page->firstRow.','.$Page->listRows;
	        $users = $user->query($sql);
	        $this->assign('users',$users);        
	        $this->assign('page',$show);// 赋值分页输出
    	}
        $this->display();
    }

    /**
     * 导出 老师列表所有数据
     */
    public function laoshi_export(){
        $sql = "select id,phone,sex,name,education,school,major,city,is_shenhe_xs,is_dongjie,create_time from laoshi_user where is_del=0";
        $data = M()->query($sql);

        $subject="Excel导出-老师列表";
        $title=array("id","老师手机","老师性别","老师","学历","学校","专业","城市","是否审核","是否冻结","加入时间");
        exportExcel($data,$title,$subject); 
    }

    //添加显示
    public function add_index()
    {
    	//城市显示
    	$citys = M('city')->where('parent_id=0')->select();
    	$this->assign('citys',$citys);

    	$this->display();
    }
    //添加
    public function add()
    {
    	//[phone] => 13430372763[username] => 111[sex] => 女[xueli] => 大专[school] => 11[zhuanye] => 111[city] => 广州[pass] => 111111[repass] => 111111
    	if (IS_POST) {
    		$data['phone'] = I('post.phone');
    		$data['name'] = I('post.username');
    		$data['sex'] = I('post.sex');
    		$data['education'] = I('post.xueli');
    		$data['school'] = I('post.school');
    		$data['major'] = I('post.zhuanye');
    		$data['city'] = I('post.city');

        //手机重复检测
        $shouji_cz = M('laoshi_user')->where(array('phone'=>$data['phone']))->getField('phone');
        if (!empty($shouji_cz)) {
          $arr = array(
            'error' =>0,
            'info' => '手机已存在'
            );
          echo json_encode($arr);
          exit;
        }


    		//默认头像
    		if ($data['sex'] == '男') {
    			$data['face'] = "/Uploads/face/man.jpg";
    		}else {
    			$data['face'] = "/Uploads/face/woman.jpg";
    		}

    		$password = I('post.pass');
    		$data['password'] = md5($password);

    		$data['create_time'] = time();
    		$data['create_ip'] =  get_client_ip();

    		$res = M('laoshi_user')->add($data);
    		if ($res) {
    			$arr = array(
    				'error' =>1,
    				'info' => '添加成功'
    				);
    			echo json_encode($arr);
    		}else {
    			$arr = array(
    				'error' =>0,
    				'info' => '添加失败'
    				);
    			echo json_encode($arr);
    		}
    	}
    }

    //编辑显示
    public function edit_index()
    {
    	//城市显示
    	$citys = M('city')->where('parent_id=0')->select();
    	$this->assign('citys',$citys);
    	
    	$id = I('get.id');
    	$sql = "select id,phone,sex,name,education,school,major,city,create_time,sfz_name,sfz_img,jiaoshizheng_img,qitazheng_img from laoshi_user where id='{$id}'";
    	$laoshi = M()->query($sql);
    	$this->assign('laoshi',current($laoshi));

    	$this->display();
    }
   //编辑
    public function edit()
    {
    	if (IS_POST) {
    		//$data['phone'] = I('post.phone');
    		$data['name'] = I('post.username');
    		$data['sex'] = I('post.sex');
    		$data['education'] = I('post.xueli');
    		$data['school'] = I('post.school');
    		$data['major'] = I('zhuanye');
    		$data['city'] = I('post.city');

    		$id = I('post.id');

    		//更新
    		$res = M('laoshi_user')->where(array('id'=>$id))->save($data);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '更新成功'    
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '更新失败'    
                    );
                echo json_encode($arr);
            }
    	}
    }
    //查看老师证件照片原图
    public function zheng_img()
    {
      $id = I('get.id');
      $img = I('get.img');
      
      if ($img == 'sfz') {
        //身份证
        $img_path = M('laoshi_user')->where(array('id'=>$id))->getField('sfz_img');
      }elseif ($img == 'jsz') {
        //教师
        $img_path = M('laoshi_user')->where(array('id'=>$id))->getField('jiaoshizheng_img');
      }elseif ($img == 'qt') {
        //其它
        $img_path = M('laoshi_user')->where(array('id'=>$id))->getField('qitazheng_img');
      }
      $this->assign('img_path',$img_path);

      $this->display();
    }

    //老师授课信息表 显示
    public function showke_info()
    {
    	$id = I('get.id');
    	$this->assign('id',$id);

    	//城市对应的区
    	$city = M('laoshi_user')->where(array('id'=>$id))->getField('city');
    	$cityid = M('city')->where(array('city_name'=>$city))->getField('cityid');
    	$city_quyu = M('city')->where(array('parent_id'=>$cityid))->select();
    	$this->assign('city_quyu',$city_quyu);

    	//授课详细信息
    	$showke_info = M('showke_info')->where(array('uid'=>$id))->find();
    	$this->assign('showke_info',$showke_info);

    	$showke_grades = explode(',', $showke_info['grades']);
    	$this->assign('showke_grades',$showke_grades);

    	$showke_methods = explode(',', $showke_info['methods']);
    	$this->assign('showke_methods',$showke_methods);

      //$showke_info['shouke_time']转数组
      $shouke_times = explode(',', $showke_info['shouke_time']);
      $this->assign('shouke_times',$shouke_times);

    	//科目
    	$subjects = M('subjects')->select();
    	$this->assign('subjects',$subjects);

    	//年级
    	$grades = M('grade')->where('parent_id=0')->select();
    	$this->assign('grades',$grades);

    	//上课方式
    	$methods = M('teachingmethod')->select();
    	$this->assign('methods',$methods);

      //授课时间段显示 上午
      $showke_time_sw = M('showke_time')->where(array('time_name'=>'上午'))->select();
      $this->assign('showke_time_sw',$showke_time_sw);
      //授课时间段显示 下午
      $showke_time_xw = M('showke_time')->where(array('time_name'=>'下午'))->select();
      $this->assign('showke_time_xw',$showke_time_xw);
      //授课时间段显示 晚上
      $showke_time_ws = M('showke_time')->where(array('time_name'=>'晚上'))->select();
      $this->assign('showke_time_ws',$showke_time_ws);

    	$this->display();
    }

    //老师授课信息表 添加/更新
    public function showke_info_add()
    {
    	if (IS_POST) {
    		$data['uid'] = I('post.uid');

    		$data['sk_city'] = I('post.sk_city');
    		$data['subject'] = I('post.subject');
    		$data['address'] = I('post.address');
    		$data['price'] = I('post.price');

        $sm_shijian = implode(',', I('post.sm_shijian'));
    		$data['shouke_time'] = $sm_shijian;

    		$grade = I('post.grade');
        //年级选择不能超过3
        $count_grade = count($grade);
        if ($count_grade > 4) {
            $arr = array(
                'error' => 0,
                'info' => '年级选择不能超过4个哦'    
                );
            echo json_encode($arr);
            exit;
        }
    		$data['grades'] = implode(',', $grade);
    		$methods = I('post.methods');
    		$data['methods'] = implode(',', $methods);

    		//入库
            //检测老师授课信息存在 跟新 不存在 添加 
            $uid = M('showke_info')->where(array('uid'=>$data['uid']))->find();
            if ($uid) {
                $res = M('showke_info')->where(array('uid'=>$data['uid']))->save($data);
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '更新成功'    
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '更新失败'    
                        );
                    echo json_encode($arr);
                }
            }else {
                $res = M('showke_info')->add($data);
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '添加成功'    
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '添加失败'    
                        );
                    echo json_encode($arr);
                }
            }
    	}
    }

    //老师教龄经历表 
    public function jiaoling_jli()
    {
      $id = I('get.id');
      $this->assign('id',$id);

      $jiaoling_jli = M('jiaoling_jli')->where(array('uid'=>$id))->find();
      $this->assign('jiaoling_jli',$jiaoling_jli);

      //学生类型
      $jiaoling_jli_stye = explode(',', $jiaoling_jli['stu_stye']);
      $this->assign('jiaoling_jli_stye',$jiaoling_jli_stye);
      //老师特点
      $jiaoling_jli_tedian = explode(',', $jiaoling_jli['laoshi_stye']);
      $this->assign('jiaoling_jli_tedian',$jiaoling_jli_tedian);
      

      $this->display();
    }
    //老师教龄经历表  添加/更新
    public function jiaoling_jli_add()
    {
      //[jiaoling] => 1[start_year] => 1980[start_month] => 3[stop_year] => 1983[stop_month] => 4[content] => 1111[jianjie] => 22222222222222[stye_one] => 基础薄弱型[stye_two] => 偏科严重型[stye_three] => 培优拔高型[tedian_one] => 因材施教[tedian_two] => 注重方法[tedian_three] => 睿智创新[uid] => 12
      if (IS_POST) {
            $data['jiaoling'] = I('post.jiaoling');

            $data['start_year'] = I('post.start_year');
            $data['start_month'] = I('post.start_month');
            $data['stop_year'] = I('post.stop_year');
            $data['stop_month'] = I('post.stop_month');

            $data['experience'] = I('post.content');
            $data['jianjie'] = I('post.jianjie');

            $stu_stye = I('post.stye_one').','.I('post.stye_two').','.I('post.stye_three');
            $data['stu_stye'] = $stu_stye;
            $laoshi_stye = I('post.tedian_one').','.I('post.tedian_two').','.I('post.tedian_three');
            $data['laoshi_stye'] = $laoshi_stye;

            $data['uid'] = I('post.uid');
            $data['create_time'] = time();

            //检测老师经历存在 跟新 不存在 添加 
            $uid = M('jiaoling_jli')->where(array('uid'=>$data['uid']))->find();
            if ($uid) {
                $res = M('jiaoling_jli')->where(array('uid'=>$data['uid']))->save($data);
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '更新成功'    
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '更新失败'    
                        );
                    echo json_encode($arr);
                }
            }else {
                $res = M('jiaoling_jli')->add($data);
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '添加成功'    
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '添加失败'    
                        );
                    echo json_encode($arr);
                }
            }
      }
    }


    //用户停用 交互
    public function user_stop()
    {
       //print_r($_POST);exit;
      if (IS_POST) {
        $userid = I('post.userid');
        $sql = 'update laoshi_user set is_dongjie=0 where id='.$userid;
        $is_dongjie = M()->execute($sql);
        if ($is_dongjie) {
            $arr = array(
                'error' =>0,
                'info'=>"用户停用成功",
                'userid' =>$userid
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' =>1,
                'info'=>"用户停用失败"
                );
            echo json_encode($arr);
        }

      }
        
    }
    //用户启用 交互
    public function user_start()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update laoshi_user set is_dongjie=1 where id='.$userid;
          $is_dongjie = M()->execute($sql);
          if ($is_dongjie) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户启用成功",
                  'userid' =>$userid
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户启用失败"
                  );
              echo json_encode($arr);
          }
      }

    }

    //删除状态 0未删除 1删除
    public function is_del()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update laoshi_user set is_del=1 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
        }       
    }

    //老师显示审核 0未审核 1审核
    public function ls_is_shenhe()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update laoshi_user set is_shenhe_xs=1 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户审核成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户审核失败"
                  );
              echo json_encode($arr);
          }
        }
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('laoshi_user')->where($w)->setField('is_del',1);
          if ($res) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }






    //删除老师列表
    public function del_index()
    {
    	if (IS_POST) {
	    	$start = strtotime(I('post.start'));
	    	$stop = strtotime(I('post.stop'));
	    	$username = I('post.username');
	    	$sql = "select id,phone,sex,name,education,school,major,city,is_dongjie,create_time from laoshi_user where is_del=1 and create_time between '{$start}' and '{$stop}' and name like '%{$username}%'";
	    	$users = M()->query($sql);
	    	$this->assign('users',$users);
    	}else {
	    	//分页
	        $count = M('laoshi_user')->count();
	        $this->assign('num',$count);

	        //所有用户显示出来
	        $user = M('laoshi_user');        
	        // 查询满足要求的总记录数
	        $count =  $user->count();
	        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
	        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

	        //问题显示
	        $sql = "select id,phone,sex,name,education,school,major,city,is_dongjie,create_time from laoshi_user where is_del=1 limit ".$Page->firstRow.','.$Page->listRows;
	        $users = $user->query($sql);
	        $this->assign('users',$users);        
	        $this->assign('page',$show);// 赋值分页输出
    	}

    	$this->display();
    }

    //批量恢复
    public function recovers()
    {
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('laoshi_user')->where($w)->setField('is_del',0);
        if ($res) {
          $arr = array(
              'error' => 0,
              'info' => '恢复成功'
            );
          echo json_encode($arr);
        }else {
          $arr = array(
              'error' => 1,
              'info' => '恢复失败'
            );
          echo json_encode($arr);
        }
    }

    //删除状态为0的用户 恢复  改为1
    public function no_del()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update laoshi_user set is_del=0 where id='.$userid;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户恢复成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户恢复失败"
                  );
              echo json_encode($arr);
          }
        }  
    }


    //彻底删除
    public function delete()
    {
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'delete from laoshi_user where id='.$userid;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户彻底删除失败"
                  );
              echo json_encode($arr);
          }
      }  
    }


    //成功案例表
    public function success()
    {
      $id = I('get.id');
      $this->assign('id',$id);

      //最近的成功案例显示
      $success = M('success_case')->where(array('uid'=>$id))->order('create_time desc')->select();
      $this->assign('success',$success);
      
      $this->display();
    }
    //成功案例
    public function success_index()
    {
      $id = I('get.id');
      $this->assign('id',$id);

      $this->display();
    }
    //成功案例 添加
    public function sussess_case_add()
    {
      if (IS_POST) {
          $data['uid'] = I('post.uid');

          $data['time_year'] = I('post.start_year');
          $data['time_month'] = I('post.start_month');
          $data['stu_name'] = I('post.name');
          $data['school'] = I('post.school');
          $data['result'] = I('post.content');
          $data['create_time'] = time();

          $res = M('success_case')->add($data);
          if ($res) {
              $arr = array(
                  'error' => 1,
                  'info' => '添加成功'    
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' => 0,
                  'info' => '添加失败'    
                  );
              echo json_encode($arr);
          }
      }
    }

    //成功案例编辑显示
    public function success_edit_index()
    {
      $id = I('get.id');
      $success = M('success_case')->where(array('id'=>$id))->find();
      $this->assign('success',$success);

      $this->display();
    }

    //成功案例 编辑
    public function success_edit()
    {
      if (IS_POST) {
          $id= I('post.id');

          $data['time_year'] = I('post.start_year');
          $data['time_month'] = I('post.start_month');
          $data['stu_name'] = I('post.name');
          $data['school'] = I('post.school');
          $data['result'] = I('post.content');

          //更新
          $res = M('success_case')->where(array('id'=>$id))->save($data);
          if ($res) {
              $arr = array(
                  'error' => 1,
                  'info' => '更新成功'    
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' => 0,
                  'info' => '更新失败'    
                  );
              echo json_encode($arr);
          }
      }
    }

    //成功案例 删除
    public function success_delete()
    {
      if (IS_POST) {
        $id = I('post.id');
        $res = M('success_case')->delete($id);
        if ($res) {
            $arr = array(
                'error' => 1,
                'info' => '删除成功'    
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0,
                'info' => '删除失败'    
                );
            echo json_encode($arr);
        }
      }
    }


    //银行卡信息
    public function bank_index()
    {
      $id = I('get.id');
      $this->assign('id',$id);

      $bank = M('bank')->where(array('uid'=>$id))->find();
      $this->assign('bank',$bank);

      $this->display();
    }
    //银行卡信息 编辑
    public function bank_edit()
    {//[bank_name] => 小明[bank_addr] => 广州天河农业银行分行[bank] => 中国农业银行[bank_num] => 1234567891011111[uid] => 2
        if (IS_POST) {
            $data['uid'] = I('post.uid');
            $data['is_who'] = 'laoshi';

            $data['bank_name'] = I('post.bank_name');
            $data['bank_addr'] = I('post.bank_addr');
            $data['bank'] = I('post.bank');
            $data['bank_num'] = I('post.bank_num');

            $str = "/^(\d{16}|\d{19})$/";
            $bank_num_yz = preg_match($str, $data['bank_num']);
             if (!$bank_num_yz) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的银行卡号'
                    );
                echo json_encode($arr);
                exit;
             }

            //检测是否绑定银行卡 存在 跟新 不存在 添加 
            $uid = M('bank')->where(array('uid'=>$data['uid']))->find();
            if ($uid) {
                $res = M('bank')->where(array('uid'=>$data['uid']))->save($data);
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '更新成功'    
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '更新失败'    
                        );
                    echo json_encode($arr);
                }
            }else {
                $res = M('bank')->add($data);
                if ($res) {
                    $arr = array(
                        'error' => 1,
                        'info' => '添加成功'    
                        );
                    echo json_encode($arr);
                }else {
                    $arr = array(
                        'error' => 0,
                        'info' => '添加失败'    
                        );
                    echo json_encode($arr);
                }
            }
        }
    }









}